package com.liuhousemyjavabasic.controller;

import io.woo.htmltopdf.HtmlToPdf;
import io.woo.htmltopdf.HtmlToPdfObject;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
public class PdfDemo {


    /**
     * 根据Html转换为pdf文件 支持多个str
     */
    @GetMapping("/html_to_pdf_1")
    private String htmlToPdf()
    {
        String str="<table>\n" +
                "    <th>\n" +
                "        <td>ID</td>\n" +
                "        <td>name</td>\n" +
                "        <td>姓名</td>\n" +
                "        <td>说明</td>\n" +
                "    </th>\n" +
                "    <tr>\n" +
                "        <td>ID</td>\n" +
                "        <td>name</td>\n" +
                "        <td>姓名</td>\n" +
                "        <td>说明</td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td>ID</td>\n" +
                "        <td>name</td>\n" +
                "        <td>姓名</td>\n" +
                "        <td>说明</td>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td>ID</td>\n" +
                "        <td>name</td>\n" +
                "        <td>姓名</td>\n" +
                "        <td>说明</td>\n" +
                "    </tr>\n" +
                "</table>";
        //linux平台的路径就要这样写
//        String rootPath = "//home//xbcpdf";
        String rootPath = "D:\\Test";
        HtmlToPdf.create().object(HtmlToPdfObject.forHtml(str).defaultEncoding("utf8"))
                .convert(rootPath + "\\" +"htmltest.pdf");
        return "OK1";
    }

    @GetMapping("/html_to_pdf_2")
    public String  urlToPdf(){
//        String rootPath = "//home//xbcpdf";
        String rootPath = "D:\\Test";
        HtmlToPdf.create()
                .object(HtmlToPdfObject.forUrl("https://www.xiaobangcai.com/prints/prints/print_order_page_k?id=788447&choice_time=1712505600&one_class_str=106,111,110,107,109,105&alias_id=279&is_space=no&is_show_specs=1&deliver_id=0&p_type=A&order_by=1&enter_delivery_name="))
                .convert(rootPath + "\\" +"htmltesturl.pdf");
        return "OK2";
    }


    @Test
    public void testPath(){
        //定义本地磁盘路径
        File file = new File("D:/Test");
        System.out.println(file.getPath());
    }
}
